#!/usr/bin/env Rscript
################################################################################
# @ Author       : Pengdong Yan
# @ Email        : yanpd01@snnu.edu.cn
# @ Encoding     : UTF-8
# @ Language     : 
# @ Date         : 2022-04-10 04:29
# @ LastEditTime : 2022-04-10 04:29
# @ Description  : 
################################################################################

df_all_soil_0 <- cbind(
    df_soil1,
    df_qpcr[, -1:-3],
    df_mh[, -1]
    ) %>% select (
        id,
        pH, "有机质（g/kg）",
        "氨态氮（mg/kg）", "硝态氮（mg/kg）", "磷酸盐（mg/kg）", "氧化钾（g/kg）",
        amoA, nxrA, narG, nirS, norB, nosZ, nifH,
        "过氧化氢酶（mL/g）", "蔗糖酶（mg/g）", "脲酶（μg/g）", "磷酸酶（P）（μg/g）"
    ) %>%
    group_by(id) %>%
    summarise_all(mean) %>%
    column_to_rownames("id")
pH <- df_all_soil_0$pH
df_all_soil_0$pH <- (pH - min(pH))/(7-min(pH)) ### 正向化

df_all_soil <- df_all_soil_0 %>%
    decostand("normalize", 2)
colSums(df_all_soil^2)
(df_wa1 <- yyread(clipboard(), rownames = T))
View(data.frame(colnames(df_all_soil), colnames(df_wa1)))
(df_all_wa <- rep(df_wa1[1, ], 8) %>% unlist() %>% matrix(byrow = T, ncol = ncol(df_wa1)))
# df_all_wa <- rep(c(2, rep(1, 16)), 8) %>% unlist() %>% matrix(byrow = T, ncol = ncol(df_wa1))




yij <- df_all_soil * df_all_wa
(y_max <-  rep(sapply(yij, max), nrow(yij)) %>% matrix(byrow = T, nrow = nrow(yij)))
(y_min <-  rep(sapply(yij, min), nrow(yij)) %>% matrix(byrow = T, nrow = nrow(yij)))
yywrite(yij, "tmp.tsv", row.names = T)
yywrite(rbind(max = y_max[1, ], min = y_min[1, ]), "tmp.tsv", row.names = T)

s_z <- (rowSums((yij - y_max)^2))^.5
s_f <- (rowSums((yij - y_min)^2))^.5
s_t <- s_f/(s_z + s_f)
(tmp_res <- rbind(
    最优解距离 = s_z,
    最劣解距离 = s_f,
    综合得分 = s_t,
    排名 = rank(-s_t)
    ))
yywrite(tmp_res, "tmp.tsv", row.names = T)